﻿using CommonDevelop.Scene;
using CommonEngine;
using ManageDto;
using ManageService;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using WebAdmin.Handler;

namespace WebAdmin.Areas.Org.Controllers
{
    /// <summary>
    /// 权限配置
    /// </summary>
    [Area("Org")]
    [Route("Org/[controller]/[action]")]
    public class PowerController : BaseController
    {
        private readonly IUserService _UserService;
        private readonly IDeptService _DeptService;
        private readonly IPermissionManager _PermissionManager;
        /// <summary>
        /// 构造注入
        /// </summary>
        public PowerController(IUserService UserService, IDeptService DeptService, IPermissionManager PermissionManager)
        {
            this._UserService = UserService;
            this._DeptService = DeptService;
            this._PermissionManager = PermissionManager;
        }

        /// <summary>
        /// 视图列表
        /// </summary>
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Title = "权限配置";
            return View();
        }

        /// <summary>
        /// 配置角色（可以多选）
        /// </summary>
        [HttpGet]
        public ActionResult GetSelectLayer()
        {
            ViewBag.Title = "配置角色";
            return View();
        }

        #region--Select--

        /// <summary>
        /// 组织树
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetOrgTree()
        {
            List<OrgDto> OrgLs = new List<OrgDto>();
            List<CompanyDto> comLs = this._DeptService.GetCompany().ToList();
            if (comLs == null)
                return this.Fail("没有企业数据");

            foreach (var com in comLs)
            {
                OrgDto OM = new OrgDto()
                {
                    NodeId = com.CompanyId,
                    ParentId = com.ParentId,
                    NodeName = com.FullName,
                    Icon = "glyphicon glyphicon-book",
                    IsCom = true
                };
                OrgLs.Add(OM);

                List<DeptDto> deptLs = this._DeptService.GetDeptByCompanyId(com.CompanyId).ToList();
                if (deptLs == null)
                    continue;

                foreach (var dept in deptLs)
                {
                    OrgDto OM2 = new OrgDto()
                    {
                        NodeId = dept.DeptId,
                        ParentId = dept.ParentId,
                        NodeName = dept.FullName,
                        Icon = "glyphicon glyphicon-bookmark",
                        IsCom = false
                    };

                    if (OM2.ParentId.IsEmpty())
                        OM2.ParentId = OM.NodeId;

                    OrgLs.Add(OM2);
                }
            }

            var Data = BizPage.GetOrgTree(OrgLs);
            return this.ToJsonContent(Data);
        }

        /// <summary>
        /// 取树节点信息
        /// </summary>
        [HttpPost]
        [AjaxOnly]
        public ActionResult GetDeptCom(string id)
        {
            ComDeptDto box = this._DeptService.GetDeptItem(id);
            if (box == null)
                return this.Fail("NULL");
            else
                return this.SuccessJson(box);
        }

        /// <summary>
        /// 视图列表查询（分页）
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetPageList(string pagination, string companyId, string deptId, string keyword)
        {
            Pagination pag = pagination.ToObject<Pagination>();
            List<UserDto> data = this._UserService.GetDtosSets(pag, companyId, deptId, keyword).ToList();
            var jsonData = new
            {
                rows = data,
                total = pag.total,
                page = pag.page,    //由前端配置
                records = pag.records
            };
            return this.SuccessJson(jsonData);
        }

        #endregion

        #region--Select2--

        /// <summary>
        /// 获取角色列表
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetRoleList(string UID)
        {
            List<RoleModel> data = this._PermissionManager.GetRoleList(UID);
            return this.SuccessJson(data);
        }

        /// <summary>
        /// 取角色配置的模块列表
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult SelectRoleModule(string RID, string UID)
        {
            List<ModuleModel> data = this._PermissionManager.GetModuleFromRole(RID);
            foreach (var v in data)
            {
                v.RoleId = RID;
                v.UserId = UID;
            }
            return this.SuccessJson(data);
        }

        /// <summary>
        /// 获取角色选择列表
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetRoleListCheck(string UID)
        {
            List<RoleModel> data = this._PermissionManager.GetRoleListCheck(UID);
            return this.SuccessJson(data);
        }

        #endregion

        #region--存储--

        /// <summary>
        /// 保存导航信息
        /// </summary>
        [HttpPost]
        [AjaxOnly]
        [DisableRequestSizeLimit]
        public ActionResult RoleConfigSave([FromForm] CollectDto Dto)
        {
            string UID = Dto.EnId;
            string Roles = Dto.ContentTxt;

            List<RoleModel> RoleData = Roles.ToList<RoleModel>();

            bool ok = this._PermissionManager.SaveURConfig(UID, RoleData);
            if (ok)
                return this.Success("保存成功");
            else
                return this.Fail("操作失败");
        }

        #endregion
    }
}